var express = require('express');
var router = express.Router();
const commodityService = require('../service/commodityServive.js');

/**
 * @swagger
 * /commodity:
 *    get:
 *      summary: 根据条件查找商品
 *      tags:
 *       - commodity
 *      parameters:
 *      - name: type
 *        in: query
 *        description: 搜索类型
 *        required: false
 *      - name: value
 *        in: query
 *        description: 搜索值
 *        required: false
 *      - name: current
 *        in: query
 *        description: 当前页
 *        required: false
 *      - name: size
 *        in: query
 *        description: 每页显示数
 *        required: false
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */

// 查询
router.get('/', async function (req, res) {
    let {
        type,
        value,
        current,
        pageSize
    } = req.query;
    console.log("type", type, value);

    let searchObj = {};
    if (type) {
        if (type == "types") {
            searchObj[type] = value
        } else {
            searchObj[type] = {
                $regex: value
            };
        }

    }
console.log("searchObj",searchObj);
    let data = await commodityService.find(searchObj, {
        current: current || 1,
        pageSize: pageSize || 15
    })
console.log(data,"返回");
    res.send({
        data
    });
});

router.get("/all", async function (req, res) {
    let data = await commodityService.findAll();
    res.send({
        data
    })
})

// 增加
/**
 * @swagger
 * /commodity:
 *    post:
 *      tags:
 *      - commodity
 *      summary: 增加商品
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                name:
 *                  type: string
 *                oldPrice:
 *                  type: string
 *                newPrice:
 *                  type: string
 *                freight:
 *                  type: string
 *                images:
 *                  type: string
 *                color: 
 *                  type: string
 *                size: 
 *                  type: string
 *                fit: 
 *                  type: string
 *                brand: 
 *                  type: string
 *                intro:
 *                  type: string
 *                stock:
 *                  type: string
 *                types:
 *                  type: string
 *
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */

router.post('/', async function (req, res) {
    let {
        name,
        oldPrice,
        newPrice,
        freight,//运费
        images,//商品图片
        intro,//商品介绍
        stock,//商品库存
        types, //商品类型
        color, //商品颜色
        size, //商品大小
        fit,//商品适用人群
        brand//品牌
    } = req.body;
    if (images != []) {
        console.log("images", images);
        // images = JSON.parse(images);
    }


    let data = await commodityService.addCommodity({
        name,
        oldPrice,
        newPrice,
        freight,//运费
        images,//商品图片
        intro,//商品介绍
        stock,//商品库存
        types, //商品类型
        color, //商品颜色
        size, //商品大小
        fit,//商品适用人群
        brand//品牌
    });
    res.send({
        data
    });
});


// 删除
/**
 * @swagger
 * /commodity/{id}:
 *    delete:
 *      tags:
 *      - commodity
 *      summary: 删除管理员
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 删除管理员id
 *        required: true
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */

router.delete('/:id', async function (req, res) {
    let {
        id
    } = req.params;
    let data = await commodityService.delCommodity(id);
    res.send({
        data
    });
});


// 修改
// 根据id查询
/**
 * @swagger
 * /commodity/{id}:
 *    get:
 *      tags:
 *      - commodity
 *      summary: 根据id查询
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 查询对象id
 *        required: true
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.get("/:id", async function (req, res) {
    let { id } = req.params;
    let data = await commodityService.commodityById(id);
    res.send({ data });
});

// 修改
/**
 * @swagger
 * /commodity/{id}:
 *    put:
 *      tags:
 *      - commodity
 *      summary: 根据id修改
 *      parameters:
 *      - name: id
 *        in: path
 *        description: 查询对象id
 *        required: true
 *      requestBody:
 *        required: true
 *        content:
 *          application/x-www-form-urlencoded:
 *            schema:
 *              type: object
 *              properties:
 *                name:
 *                  type: string
 *                oldPrice:
 *                  type: string
 *                newPrice:
 *                  type: string
 *                freight:
 *                  type: string
 *                images:
 *                  type: string
 *                spec:
 *                  type: object
 *                params:
 *                  type: object
 *                intro:
 *                  type: string
 *                stock:
 *                  type: string
 *                types:
 *                  type: string
 *
 *      responses:
 *        200:
 *          description: 操作成功
 *        401:
 *          description: 权限不足
 * */
router.put('/:id', async function (req, res) {
    let {
        id
    } = req.params;
    let {
        name,
        oldPrice,
        newPrice,
        freight,//运费
        images,//商品图片
        intro,//商品介绍
        stock,//商品库存
        types, //商品类型
        color, //商品颜色
        size, //商品大小
        fit,//商品适用人群
        brand//品牌
    } = req.body;
    let data = await commodityService.updateCommodity(id, {
        name,
        oldPrice,
        newPrice,
        freight,//运费
        images,//商品图片
        intro,//商品介绍
        stock,//商品库存
        types, //商品类型
        color, //商品颜色
        size, //商品大小
        fit,//商品适用人群
        brand//品牌
    });
    res.send({
        data
    });
});

module.exports = router;